//package com.hkbigdata.sybx.controller;
//
//
//import com.hkbigdata.sybx.bean.Admin;
//import com.hkbigdata.sybx.service.AdminService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import java.util.List;
//
///**
// * @author yangjing
// * @creat 2025-05-07-11:42
// * @email 3010335359@qq.com
// */
//@RestController
//@RequestMapping("/api/admin")
//public class AdminController {
//    @Autowired
//    private AdminService adminService;
//
//    @GetMapping("/all")
//    public List<Admin> getAllUsers() {
//        return adminService.getAllUsers();
//    }
//    //        登录用户
//    @PostMapping("/login")
//    public String login(@RequestBody Admin admin) {
//        Admin dbAdmin = adminService.getAdminBeanByAccount(admin.getAdmin_phone());
//        if (dbAdmin != null && dbAdmin.getAdmin_password().equals(admin.getAdmin_password())) {
//            return "登录成功";
//        }
//        return "登录失败";
//    }
//
//}



package com.hkbigdata.sybx.controller;

import com.hkbigdata.sybx.bean.Admin;
import com.hkbigdata.sybx.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangjing
 * @creat 2025-05-07-11:42
 * @email 3010335359@qq.com
 */
@RestController
@RequestMapping("/api/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/all")
    public List<Admin> getAllUsers() {
        return adminService.getAllUsers();
    }

    // 管理员登录
    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Admin admin) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 根据手机号查找管理员
            Admin dbAdmin = adminService.getAdminBeanByAccount(admin.getAdmin_phone());

            if (dbAdmin != null && dbAdmin.getAdmin_password().equals(admin.getAdmin_password())) {
                // 登录成功
                result.put("success", true);
                result.put("message", "管理员登录成功");
                result.put("admin", dbAdmin);

                // 生成JWT令牌 (实际项目中应该实现)
                // String token = jwtService.generateToken(dbAdmin.getAdminId(), "admin");
                // result.put("token", token);

                // 为了演示，使用固定令牌
                result.put("token", "admin-token-demo-123456");
            } else {
                // 登录失败
                result.put("success", false);
                result.put("message", "账号或密码错误");
            }
        } catch (Exception e) {
            // 登录异常
            result.put("success", false);
            result.put("message", "登录过程发生异常: " + e.getMessage());
            // 记录日志
            e.printStackTrace();
        }

        return result;
    }

    // 验证管理员令牌 (示例接口)
    @GetMapping("/validateToken")
    public Map<String, Object> validateToken(@RequestHeader("Authorization") String token) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 实际项目中应该验证令牌有效性
            // boolean isValid = jwtService.validateToken(token);

            // 为了演示，假设令牌有效
            boolean isValid = token != null && token.startsWith("admin-token");

            if (isValid) {
                result.put("success", true);
                result.put("message", "令牌有效");
            } else {
                result.put("success", false);
                result.put("message", "无效令牌");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "令牌验证异常: " + e.getMessage());
            e.printStackTrace();
        }

        return result;
    }
    // 新增：删除管理员
    @DeleteMapping("/{admin_phone}")
    public Map<String, Object> deleteAdmin(@PathVariable String admin_phone) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 检查管理员是否存在
            Admin admin = adminService.getAdminBeanByAccount(admin_phone);
            if (admin == null) {
                result.put("success", false);
                result.put("message", "管理员不存在");
                return result;
            }

            // 执行删除
            int rows = adminService.deleteAdmin(admin_phone);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "管理员删除成功");
            } else {
                result.put("success", false);
                result.put("message", "管理员删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除过程发生异常: " + e.getMessage());
            e.printStackTrace();
        }

        return result;
    }

    // 新增：修改管理员信息
    @PutMapping("/{admin_phone}")
    public Map<String, Object> updateAdmin(@PathVariable String admin_phone, @RequestBody Admin admin) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 检查管理员是否存在
            Admin existingAdmin = adminService.getAdminBeanByAccount(admin_phone);
            if (existingAdmin == null) {
                result.put("success", false);
                result.put("message", "管理员不存在");
                return result;
            }

            // 设置要更新的管理员手机号
            admin.setAdmin_phone(admin_phone);

            // 执行更新
            int rows = adminService.updateAdmin(admin);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "管理员信息更新成功");
            } else {
                result.put("success", false);
                result.put("message", "管理员信息更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新过程发生异常: " + e.getMessage());
            e.printStackTrace();
        }

        return result;
    }
    @PostMapping("/add")
    public Map<String, Object> addAdmin(@RequestBody Admin admin) {
        Map<String, Object> result = new HashMap<>();

        try {
            // 检查账号是否已存在（保留唯一性验证）
            Admin existingAdmin = adminService.getAdminBeanByAccount(admin.getAdmin_phone());
            if (existingAdmin != null) {
                result.put("success", false);
                result.put("message", "该账号已存在");
                return result;
            }

            // 执行添加（移除手机号和密码格式验证）
            int rows = adminService.addAdmin(admin);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "管理员添加成功");
            } else {
                result.put("success", false);
                result.put("message", "管理员添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "添加过程发生异常: " + e.getMessage());
            e.printStackTrace();
        }

        return result;
    }
}